package ${serviceImplPath};
import ${poPath}.${classInfo.className};
import ${converterPath}.${classInfo.className}Converter;
import ${queryPath}.${classInfo.className}Query;
import ${modePath}.${classInfo.className}Co;
import com.bicai.common.dto.Response;
import com.yacai.product.common.util.ResultCode;
import ${servicePath}.${classInfo.className}Service;
import ${repositoryPath}.${classInfo.className}Repository;

import org.mapstruct.factory.Mappers;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ${classInfo.className}
 * @Description ${classInfo.classComment}
 * @Author ${authorName}
 * @Email ${authorEmail}
 * @Date '${.now?string('yyyy-MM-dd HH:mm:ss')}'
 */
@Service
public class ${classInfo.className}ServiceImpl implements ${classInfo.className}Service {

    private ${classInfo.className}Converter ${classInfo.className?uncap_first}Converter = Mappers.getMapper(${classInfo.className}Converter.class);

	@Resource
	private ${classInfo.className}Repository ${classInfo.className?uncap_first}Repository;


	/**
    * 新增
    */
	@Override
	public Response<String> insert(${classInfo.className}Query  ${classInfo.className?uncap_first}Query) {

		// valid
		if (${classInfo.className?uncap_first}Query == null) {
            return Response.failure(ResultCode.MISSING_ESSENTIAL_PARAMETERS);
        }

		${classInfo.className?uncap_first}Repository.insert(${classInfo.className?uncap_first}Converter.queryToData(${classInfo.className?uncap_first}Query));
        return Response.success();
	}

	/**
	* 删除
	*/
	@Override
	public Response<String> delete(int id) {
		int ret = ${classInfo.className?uncap_first}Repository.delete(id);
		return ret>0?Response.success():Response.failure();
	}

	/**
	* 更新
	*/
	@Override
	public Response<String> update(${classInfo.className}Query  ${classInfo.className?uncap_first}Query) {
        // valid
        if (${classInfo.className?uncap_first}Query == null) {
        return Response.failure(ResultCode.MISSING_ESSENTIAL_PARAMETERS);
        }
		int ret = ${classInfo.className?uncap_first}Repository.update(${classInfo.className?uncap_first}Converter.queryToData(${classInfo.className?uncap_first}Query));
		return ret>0?Response.success():Response.failure();
	}

	/**
	* Load查询
	*/
	@Override
	public Response<${classInfo.className}Co> load(int id) {
		return Response.success(${classInfo.className?uncap_first}Converter.dataToClient(${classInfo.className?uncap_first}Repository.load(id)));
	}

	/**
	* 分页查询
	*/
	@Override
	public Map<String,Object> pageList(int offset, int pagesize) {

		List<${classInfo.className}> pageList = ${classInfo.className?uncap_first}Repository.pageList(offset, pagesize);
        List<${classInfo.className}Co> ${classInfo.className?uncap_first}Cos = ${classInfo.className?uncap_first}Converter.dataToClientList(pageList);
		int totalCount = ${classInfo.className?uncap_first}Repository.pageListCount(offset, pagesize);

		// result
		Map<String, Object> result = new HashMap<String, Object>();
        result.put("pageList", ${classInfo.className?uncap_first}Cos);
        result.put("totalCount", totalCount);

		return result;
	}

}
